/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ftpcombiner.gui.applicationframe.framesplitter.filetable;

import ftpcombiner.ApplicationController;
import ftpcombiner.gui.GUILoader;
import ftpcombiner.listeners.CreateFolderListener;
import ftpcombiner.listeners.DeleteListener;
import ftpcombiner.listeners.DownloadListener;
import ftpcombiner.listeners.UploadListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTable;

/**
 *
 * @author Ben
 */
public class FileContextMenu extends JPopupMenu implements GUILoader {

    private JTable parent;
    private JMenuItem uploadItem, downloadItem, deleteItem, renameItem, newFolderItem;
    private JMenu newMenu;

    public FileContextMenu(JTable parent) {
        this.parent = parent;
        init();
    }

    private void init() {
        initializeComponents();
        setLayouts();
        addComponents();
        addComponentListeners();
        otherInit();
    }

    @Override
    public void initializeComponents() {
        uploadItem = new JMenuItem("Upload...");
        downloadItem = new JMenuItem("Download");
        deleteItem = new JMenuItem("Delete");
        renameItem = new JMenuItem("Rename");
        newMenu = new JMenu("New");
        newFolderItem = new JMenuItem("Folder");
    }

    @Override
    public void setLayouts() {
    }

    @Override
    public void addComponents() {
        add(uploadItem);
        add(downloadItem);
        add(renameItem);
        add(deleteItem);
        add(newMenu);
        newMenu.add(newFolderItem);
    }

    @Override
    public void addComponentListeners() {
        parent.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {

                if (e.isPopupTrigger()) {
                    if (0 > Arrays.binarySearch(parent.getSelectedRows(), parent.rowAtPoint(e.getPoint()))) {
                        ((JTable) parent).changeSelection(parent.rowAtPoint(e.getPoint()), 0, false, false);
                    }
                    show(parent, e.getX(), e.getY());
                }
            }

            @Override
            public void mousePressed(MouseEvent e) {

                if (e.isPopupTrigger()) {
                    if (0 > Arrays.binarySearch(parent.getSelectedRows(), parent.rowAtPoint(e.getPoint()))) {
                        ((JTable) parent).changeSelection(parent.rowAtPoint(e.getPoint()), 0, false, false);
                    }
                    show(parent, e.getX(), e.getY());
                }
            }
        });
        downloadItem.addMouseListener(new DownloadListener());
        uploadItem.addMouseListener(new UploadListener());
        renameItem.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                //System.out.println("rename");
                if(parent.getSelectedRowCount() == 1){
                    parent.editCellAt(parent.getSelectedRow(), 0);
                }
            }
        });
        deleteItem.addMouseListener(new DeleteListener());
        newFolderItem.addMouseListener(new CreateFolderListener());
    }

    @Override
    public void otherInit() {
        ApplicationController.getSplashScreen().incrementProgress();
    }
}
